(function (window) {
	'use strict';

	// Your starting point. Enjoy the ride!
	
	
	
	var app = angular.module("app" , ["server"]);
	
	app.controller("appcontroller",function($scope,$location,myServer){
		
	  var todos = $scope.todos = myServer.get();
		
		$scope.newtodo = ""
		$scope.add = function () {
			if(!$scope.newtodo) return		
			myServer.add($scope.newtodo)
			$scope.newtodo = ""
	
		}
		
		$scope.isediting = -1;
		
		$scope.change = function (id) {
			$scope.isediting = id;
		}
		
		$scope.save = function () {
			$scope.isediting = -1;
		}
		
		$scope.dele = function (id) {
			myServer.del(id)
			
//			for (var i= $scope.todos.length-1;i>=0;i--) {
//				if(id === $scope.todos[i].id){
//					$scope.todos.splice( i ,1);
//					return
//				}
//			}
			
		}
		$scope.deleAll = function () {
//			for (var i= $scope.todos.length-1;i>=0;i--) {
//				if($scope.todos[i].completed){
//					$scope.todos.splice( i ,1);
//				}
//			}
			
			myServer.deleAll()
		}
		
		$scope.toggle = false;
		$scope.togglecompleted = function (){
//					for (var i= $scope.todos.length-1;i>=0;i--) {
//						$scope.todos[i].completed = $scope.toggle;
//					}
//				$scope.toggle = !$scope.toggle;
			myServer.togglecompleted($scope.toggle);
		}
		
		$scope.getnum = function() {
			$scope.num = 0;
			for (var i= $scope.todos.length-1;i>=0;i--) {
				$scope.todos[i].completed ? "" :$scope.num++ 
			}
			return $scope.num;
		}
		
		$scope.show = function (){
			
		   return  $scope.todos.length - $scope.getnum() == 0 ?  false : true;   
		}
//		$scope.status = {};
//		
//		$scope.all = function () {
//			$scope.status = {};
//		}
//	
//		$scope.active = function () {
//			$scope.status.completed = false;
//		}
//	
//		$scope.complete = function () { 
//			$scope.status.completed = true;
//		}
//		console.log($location)
        $scope.Url = $location;
        
        $scope.$watch("Url.url()",function(now,old){
         $scope.status = now ==="/active" ?  {completed :false} : (now === "/completed" ? {completed : true }: {} )
        })
		
	})
		
    app.directive('todoFocus', function todoFocus($timeout) {
		'use strict';

		return function (scope, elem, attrs) {
			scope.$watch(attrs.todoFocus, function (newVal) {
				if (newVal) {
					$timeout(function () {
						elem[0].focus();
					}, 0, false);
				}
			});
		};
	});

    
    
})(window);


(function(window){
	 var server = angular.module("server",[]);
   
  	 server.service("myServer",["$window",function($window){
   	
   	     var str = $window.localStorage.getItem('mytodos')|| '[]'
   		 var todos  = JSON.parse(str)

    // 1.返回任务数据
    this.get = function(){
      return todos
    }

    // 2.添加数据
    this.add = function(newTodo){
       // 把新任务添加到todos中去
      todos.push({
        id:Math.random(),
        name:newTodo,
        completed:false
      })
      this.save()
    }
   	
   	 // 保存数据，保存到localStorage中
   	this.save = function(){
// 		console.log(todos)
     	var str = JSON.stringify(todos)
        $window.localStorage.setItem('mytodos',str)
    }
     
    this.del = function (id){
    	
    	todos.forEach(function(v , i){
    		v.id === id && todos.splice( i ,1);
    	})
         this.save() 	
    }


	this.deleAll = function () {
		todos.forEach(function(v,i){
			v.completed &&  todos.splice( i ,1);
		})		
	}
   	this.togglecompleted = function (toggle) {
   		todos.forEach(function(v,i){
   			v.completed = toggle
   		})
   		this.save()
   	}
   }])
	
	
	

	
	
	
	
})(window);
